<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text" id="ipt">
   <h2 id="username"> {user.name}</h2>
</body>
<script>
    let user = {};
    let value = '';

    let ipt = document.querySelector('#ipt')
    let username = document.querySelector('#username')

    Object.defineProperty(user,'username',{
        set(newValue){
            console.log('设置值')
            value = newValue;
            ipt.value = newValue;
            username.innerHTML = newValue;
        },
        get(){
            console.log('获取值')
            return value
        }
    })

    ipt.addEventListener('inupt',(e)=>{
        user.username = e.target.value
    })
</script>
</html>